<?php
namespace Home\Controller;

class CodebmController extends CommonController {
	
public function __construct() {
		parent::__construct ();
		parent::__config ( array (
				'model_name'=>'Codebm',
		) );
	}
	
	public function index(){
		parent::__config ( array (
				'sort'=>'pro',
				'show'=>false,
		) );
		parent::index ();
		return $this->list;
	}
	
	//获取开奖结果
	function getResult(){
		$codeModel = D("Codebm");
		$vo = $codeModel->order("date desc")->find();
		return $vo;
	}
	
	function js_getdate(){
		$code_file = file_get_contents('http://zhibo.788kj.com:8889/js/i1i1i1i1i1l1l1l1l0.js');
		$code_file=json_decode($code_file,true);
		$code_array = explode(',', $code_file['k']);
		$code_result['code_array']=$code_array;
		$this->ajaxReturn($code_result);
// 		dump($code_result);
	}
	
	function js_update(){
		$num_array49 = array('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24',
		'25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49');
		
		//向该网站请求结果
// 		$code_file = file_get_contents('http://z.88kj.com/js/i1i1i1i1i1l1l1l1l0.js');
// 		$code_file = file_get_contents('http://zhibo.788kj.com:8889/js/i1i1i1i1i1l1l1l1l0.js');
		$opts = array(
				'http'=>array(
						'method'=>"GET",
						'timeout'=>1,//单位秒
				)
		);
		$cnt=0;
		while($cnt<3 && ($code_file=file_get_contents("http://zhibo.788kj.com:8889/js/i1i1i1i1i1l1l1l1l0.js", false, stream_context_create($opts)))===FALSE) $cnt++;
		
// 		dump($code_file);
// 		dump(S('session_code_file'));
// 		dump(S('new_code_result'));
// 		die();
		$session_code_file = $code_file;
		//如果请求到数据相同，则返回最新结果，而不是空
// 		 if(S('session_code_file')==$code_file){
// 			$this->ajaxReturn(S('new_code_result'));//缓存的最新结果
// 		} 
// 		if(!$code_file){
// 			$code_result['code']=null;
// 			$this->ajaxReturn($code_result);
// 		}
		$code_file=json_decode($code_file,true);
		$code_array = explode(',', $code_file['k']);
		$code_array = array('078','30','07','02','08','10','06','28','074','05','30','二','21点31分');
// 		$code_array = array('063','23','28','45','18','36','04','33','064','05','30','二','21点31分');
// 		$code_array = array('146','00','00','00','00','00','00','00','147','12','18','日','21点31分');
		
		//查结果是否已经更新，也可以做成缓存
// 		$result = S('codeinfo');
// 		dump($result);
// 		dump($code_array);
// 		die();
		//如果有结果，则表示还没更新
// 		if($result['date_num']==$code_array[0]&&$result['p_1']==$code_array[1]&&$result['p_2']==$code_array[2]&&
// 		$result['p_3']==$code_array[3]&&$result['p_4']==$code_array[4]&&$result['p_5']==$code_array[5]&&$result['p_6']==$code_array[6]&&
// 		$result['tm']==$code_array[7]){
// 			$code_result['code']=null;
// 		}else{//否则表示已开奖，则需用插入数据库，统计投票结果
			//插入奖
			//如果开了第一位
			if(in_array($code_array[1], $num_array49)){
				$p1 = getNumberAll($code_array[1],false)[0];
				$data['p_1']=$p1['number'];
				$data['x_1']=$p1['sx'];
				$data['b_1']=$p1['bs'];
			}else{
				$data['p_1']='00';
				$data['x_1']='00';
				$data['b_1']='00';
			}
			//如果开了第二位
			if(in_array($code_array[2], $num_array49)){
				$p2 = getNumberAll($code_array[2],false)[0];
				$data['p_2']=$p2['number'];
				$data['x_2']=$p2['sx'];
				$data['b_2']=$p2['bs'];
			}else{
				$data['p_2']='00';
				$data['x_2']='00';
				$data['b_2']='00';
			}
			//如果开了第三位
			if(in_array($code_array[3], $num_array49)){
				$p3 = getNumberAll($code_array[3],false)[0];
				$data['p_3']=$p3['number'];
				$data['x_3']=$p3['sx'];
				$data['b_3']=$p3['bs'];
			}else{
				$data['p_3']='00';
				$data['x_3']='00';
				$data['b_3']='00';
			}
			//如果开了第三位
			if(in_array($code_array[4], $num_array49)){
				$p4 = getNumberAll($code_array[4],false)[0];
				$data['p_4']=$p4['number'];
				$data['x_4']=$p4['sx'];
				$data['b_4']=$p4['bs'];
			}else{
				$data['p_4']='00';
				$data['x_4']='00';
				$data['b_4']='00';
			}
			//如果开了第三位
			if(in_array($code_array[5], $num_array49)){
				$p5 = getNumberAll($code_array[5],false)[0];
				$data['p_5']=$p5['number'];
				$data['x_5']=$p5['sx'];
				$data['b_5']=$p5['bs'];
			}else{
				$data['p_5']='00';
				$data['x_5']='00';
				$data['b_5']='00';
			}
			//如果开了第六位
			if(in_array($code_array[6], $num_array49)){
				$p6 = getNumberAll($code_array[6],false)[0];
				$data['p_6']=$p6['number'];
				$data['x_6']=$p6['sx'];
				$data['b_6']=$p6['bs'];
			}else{
				$data['p_6']='00';
				$data['x_6']='00';
				$data['b_6']='00';
			}
			//如果开了特码
			if(in_array($code_array[7], $num_array49)){
				$tm = getNumberAll($code_array[7],true)[0];
				$_POST['date']=date("Y-m-d ",time());
				$data['date']=$_POST['date'];
				$temp_y = substr($_POST['date'],0,4);
				$temp_m = substr($_POST['date'],5,2);
				$temp_r = substr($_POST['date'],8,2);
				$data['new_date']=$temp_y.'年'.$temp_m.'月'.$temp_r.'日';
				$data['old_date']= $tm['old_time'][4].'月'.$tm['old_time'][5].'日';
				$data['date_num']=$code_array[0];
				$data['code_key']=$temp_y.$code_array[0];
				//星期
				$rows=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
				$week_num = date("w",strtotime($_POST['date']));//星期
				$data['week']=$rows[$week_num];
				$data['tm']=$tm['number'];
				$data['sx']=$tm['sx'];
				$data['ds']=$tm['ds'];
				$data['bs']=$tm['bs'];
				$data['dx']=$tm['dx'];
				$data['wx']=$tm['wx'];
				$data['tt']=$tm['tt'];
				$data['ws']=$tm['ws'];
				$data['hds']=$tm['hds'];
				$data['jy']=$tm['jy'];
				$data['ms']=$tm['ms'];
				$data['dw']=$tm['dw'];
				$data['yy']=$tm['yy'];
				$data['td']=$tm['td'];
				$data['jx']=$tm['jx'];
				$data['hb']=$tm['hb'];
				$data['sex']=$tm['sex'];
				$data['bh']=$tm['bh'];
				$data['nv']=$tm['nv'];
				$zh = $code_array[1]+$code_array[2]+$code_array[3]+$code_array[4]+$code_array[5]+$code_array[6]+$code_array[7];
				if($zh<=175){
					$data['zh']='('.$zh.')≤176';
				}else {
					$data['zh']='('.$zh.')≥175';
				}
				$zh_gw = substr($zh,-1);//总和个位
				if(($zh_gw%2)==0){
					$data['zhds']='双';
				}else{
					$data['zhds']='单';
				}
			}else{
				$_POST['date']=date("Y-m-d ",time());
				$data['date']=$_POST['date'];
				$temp_y = substr($_POST['date'],0,4);
				$temp_m = substr($_POST['date'],5,2);
				$temp_r = substr($_POST['date'],8,2);
				$data['new_date']=$temp_y.'年'.$temp_m.'月'.$temp_r.'日';
				$data['old_date']= '00';
				$data['date_num']=$code_array[0];
				$data['code_key']=$temp_y.$code_array[0];
				//星期
				$rows=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
				$week_num = date("w",strtotime($_POST['date']));//星期
				$data['week']=$rows[$week_num];
				$data['tm']='00';
				$data['sx']='00';
				$data['ds']='00';
				$data['bs']='00';
				$data['dx']='00';
				$data['wx']='00';
				$data['tt']='00';
				$data['ws']='00';
				$data['hds']='00';
				$data['jy']='00';
				$data['ms']='00';
				$data['dw']='00';
				$data['yy']='00';
				$data['td']='00';
				$data['jx']='00';
				$data['hb']='00';
				$data['sex']='00';
				$data['bh']='00';
				$data['nv']='00';
				$data['zh']='00';
				$data['zhds']='00';
			}
			
			$codeModel =D("Codebm");
			$where_find['code_key']=$temp_y.$code_array[0];
			$result_find = $codeModel->where($where_find)->find();
			if($result_find){
				$data['id']=$result_find['id'];
				$result_save = $codeModel->save($data);
			}else{
				$result_add = $codeModel->add($data);
				$data['id']=$result_add;
			}
			dump($data);
// 			$code_result['code']=$data;
			
// 		}
// 		$code_result['code_array']=$code_array;
// 		S('code_array',$code_array);//最新请求处理过数据
// 		S('session_code_file',$session_code_file);//最新的请求原数据
// 		S('new_code_result',$code_result,604800);//将最新结果缓存下来
// 		$this->ajaxReturn($code_result);
	}
	
	
	function nextDate(){
				$code_file = file_get_contents('http://zhibo.788kj.com:8889/js/i1i1i1i1i1l1l1l1l0.js');
				$code_file=json_decode($code_file,true);
				$code_array = explode(',', $code_file['k']);
				S('code_nextDate',$code_array);
				$this->ajaxReturn($code_array);
	}
	
// 	function code(){
// // 		if(S('new_code_result')['code']){
// // 			$result = S('new_code_result')['code'];
// // 		}else{
// 			$codeModel =D("Codebm");
// 			//找结果是否已经更新，也可以做成缓存
// 			$result = $codeModel->order('date desc')->find();
// 			S('codeinfo',$result,604800);
// // 		}
// 		$this->code=$result;
// 		$this->code_array=S('code_nextDate');
// 		$this->display();
// 	}
	
	
	
	function add() {
	}
	function insert() {
	}
	
	function foreverdelete() {
	}
	
	function edit() {
	}
	
	function update() {
	}
	
	function bean() {
	}
	
	function sort() {
	}
	
	function excelImport(){
		parent::__config ( array (
				'display' => 'index' ,
				'display_parms'=>array('table'=>1),
				'excel'=>array('import'=>array('A'=>'name','B'=>'number','C'=>array('phone','phone'),'D'=>'position','E'=>'department',),
						'data'=>array('password'=>'123456','updateTime'=>date ('Y-m-d H:i:s', time()),'createTime'=>date ('Y-m-d H:i:s', time()))),
		) );
		parent::excelImport();
	}
	
	function excelOutput(){
	}
	
	function test(){
		$testModel = D("Test");
		$data['name']=date('Y-m-d H:i:s',time());
		$result = $testModel->add($data);
		dump($result);
	}
	
	//统计奖金
	function count_jj(){
		if(I('sig') != 'dobyadmin'){
			die();
		}
		
		//检查是不是自动任务，参数为空则是自动任务
		$year_month = I("year_month");//确定统计时间
		if($year_month==null||$year_month==''){
			//检查到时间统计没，获取下期开奖时间，如果不与当前月相同
// 			$code_file = file_get_contents('http://zhibo.788kj.com:8889/js/i1i1i1i1i1l1l1l1l0.js');
			$opts = array(
					'http'=>array(
							'method'=>"GET",
							'timeout'=>1,//单位秒
					)
			);
			$cnt=0;
			while($cnt<3 && ($code_file=file_get_contents("http://zhibo.788kj.com:8889/js/i1i1i1i1i1l1l1l1l0.js", false, stream_context_create($opts)))===FALSE) $cnt++;
			$session_code_file = $code_file;
			
			$code_file=json_decode($code_file,true);
			$code_array = explode(',', $code_file['k']);
			$month_next=$code_array[9];//下一期开奖励月份
			$month_now=date("m",time());//当前月
			if($month_next==$month_now){
				return ;
			}else {
				$year_month=date('Y-m',time());
			}
			dump("ok");
		}
		
		//开启事务
		$commint=true;//判断是否需要回滚
		$model = new \Think\Model ();
		$model->startTrans ();
		
		$jjggModel = D("Jjgg");
		$ranking_arr = array('第一名','第二名','第三名','第四名','第五名','第六名','第七名','第八名');
		$money_upx = array('1500','1200','1000','500','400','300','200','100');
		$money_utx = array('2000','1500','1000','500','400','300','200','100');
		$money_utm = array('5000','3000','2000','500','400','300','200','100');
		$money_utmdx = array('2000','1500','1000','500','400','300','200','100');
		$money_utmbs = array('3000','2000','1000','500','400','300','200','100');
		$money_utmds = array('2000','1500','1000','500','400','300','200','100');
		//平肖统计
		$upxModel = D("UPx");
		$where_upx['code_date']=array('neq',0);
		$where_upx['valid']=array('eq',1);
		$where_upx['code_date']=array('like',$year_month."%");
		$list_upx = $upxModel->where($where_upx)->order("pro desc,txttime asc ")->page('1,8')->select();
		foreach ($list_upx as $key_upx => $value_upx) {
			$data_upx['y_m']=$year_month;
			$data_upx['type']='px';
			$data_upx['ranking']=$ranking_arr[$key_upx];
			$data_upx['ranking_num']=$key_upx+1;
			$data_upx['username']=$value_upx['username'];
			$data_upx['uid']=$value_upx['uid'];
			$data_upx['pro']=$value_upx['pro'];
			if($data_upx['pro']<80&&$data_upx['ranking_num']<4){
				$data_upx['money']=$money_upx[$key_upx]/2;
			}else{
				$data_upx['money']=$money_upx[$key_upx];
			}
			$data_upx['createtime']=date("Y-m-d H:i:s",time());
			$where_upx_find['y_m']=$year_month;
			$where_upx_find['type']='px';
			$where_upx_find['ranking']=$ranking_arr[$key_upx];
			$find_upx = $jjggModel->where($where_upx_find)->find();
			if($find_upx){
				$where_upx_save['id']=$find_upx['id'];
				$save_upx_result=$jjggModel->where($where_upx_save)->save($data_upx);
				if($save_upx_result===false){$commint = false;}
			}else{
				$add_upx_result  = $jjggModel->add($data_upx);
				if($add_upx_result===false){$commint = false;}
			}
		}
			//特肖统计
			$utxModel = D("UTx");
			$where_utx['code_date']=array('neq',0);
			$where_utx['valid']=array('eq',1);
			$where_utx['txttime']=array('like',$year_month."%");
			$list_utx = $utxModel->where($where_utx)->order("pro desc ,txttime asc")->page('1,8')->select();
			foreach ($list_utx as $key_utx => $value_utx) {
				$data_utx['y_m']=$year_month;
				$data_utx['type']='tx';
				$data_utx['ranking']=$ranking_arr[$key_utx];
				$data_utx['ranking_num']=$key_utx+1;
				$data_utx['username']=$value_utx['username'];
				$data_utx['uid']=$value_utx['uid'];
				$data_utx['pro']=$value_utx['pro'];
				if($data_utx['pro']<80&&$data_utx['ranking_num']<4){
					$data_utx['money']=$money_utx[$key_utx]/2;
				}else{
					$data_utx['money']=$money_utx[$key_utx];
				}
				
				$data_utx['createtime']=date("Y-m-d H:i:s",time());
				$where_utx_find['y_m']=$year_month;
				$where_utx_find['type']='tx';
				$where_utx_find['ranking']=$ranking_arr[$key_utx];
				$find_utx = $jjggModel->where($where_utx_find)->find();
				if($find_utx){
					$where_utx_save['id']=$find_utx['id'];
					$save_utx_result=$jjggModel->where($where_utx_save)->save($data_utx);
					if($save_utx_result===false){$commint = false;}
				}else{
					$add_utx_result  = $jjggModel->add($data_utx);
					if($add_utx_result===false){$commint = false;}
				}
			}
			
			//特码统计
			$utmModel = D("UTm");
			$where_utm['code_date']=array('neq',0);
			$where_utm['valid']=array('eq',1);
			$where_utm['txttime']=array('like',$year_month."%");
			$list_utm = $utmModel->where($where_utm)->order("pro desc ,txttime asc ")->page('1,8')->select();
			foreach ($list_utm as $key_utm => $value_utm) {
				$data_utm['y_m']=$year_month;
				$data_utm['type']='tm';
				$data_utm['ranking']=$ranking_arr[$key_utm];
				$data_utm['ranking_num']=$key_utm+1;
				$data_utm['username']=$value_utm['username'];
				$data_utm['uid']=$value_utm['uid'];
				$data_utm['pro']=$value_utm['pro'];
				if($data_utm['pro']<80&&$data_utm['ranking_num']<4){
					$data_utm['money']=$money_utm[$key_utm]/2;
				}else{
					$data_utm['money']=$money_utm[$key_utm];
				}
				$data_utm['createtime']=date("Y-m-d H:i:s",time());
				$where_utm_find['y_m']=$year_month;
				$where_utm_find['type']='tm';
				$where_utm_find['ranking']=$ranking_arr[$key_utm];
				$find_utm = $jjggModel->where($where_utm_find)->find();
				if($find_utm){
					$where_utm_save['id']=$find_utm['id'];
					$save_utm_result=$jjggModel->where($where_utm_save)->save($data_utm);
					if($save_utm_result===false){$commint = false;}
				}else{
					$add_utm_result  = $jjggModel->add($data_utm);
					if($add_utm_result===false){$commint = false;}
				}
			}
			
			//特码大小统计
			$utmdxModel = D("UTmdx");
			$where_utmdx['code_date']=array('neq',0);
			$where_utmdx['valid']=array('eq',1);
			$where_utmdx['txttime']=array('like',$year_month."%");
			$list_utmdx = $utmdxModel->where($where_utmdx)->order("pro desc ,txttime asc")->page('1,8')->select();
			foreach ($list_utmdx as $key_utmdx => $value_utmdx) {
				$data_utmdx['y_m']=$year_month;
				$data_utmdx['type']='tmdx';
				$data_utmdx['ranking']=$ranking_arr[$key_utmdx];
				$data_utmdx['ranking_num']=$key_utmdx+1;
				$data_utmdx['username']=$value_utmdx['username'];
				$data_utmdx['uid']=$value_utmdx['uid'];
				$data_utmdx['pro']=$value_utmdx['pro'];
				if($data_utmdx['pro']<80&&$data_utmdx['ranking_num']<4){
					$data_utmdx['money']=$money_utmdx[$key_utmdx]/2;
				}else{
					$data_utmdx['money']=$money_utmdx[$key_utmdx];
				}
				
				$data_utmdx['createtime']=date("Y-m-d H:i:s",time());
				$where_utmdx_find['y_m']=$year_month;
				$where_utmdx_find['type']='tmdx';
				$where_utmdx_find['ranking']=$ranking_arr[$key_utmdx];
				$find_utmdx = $jjggModel->where($where_utmdx_find)->find();
				if($find_utmdx){
					$where_utmdx_save['id']=$find_utmdx['id'];
					$save_utmdx_result=$jjggModel->where($where_utmdx_save)->save($data_utmdx);
					if($save_utmdx_result===false){$commint = false;}
				}else{
					$add_utmdx_result  = $jjggModel->add($data_utmdx);
					if($add_utmdx_result===false){$commint = false;}
				}
			}
			//特码波色统计
			$utmbsModel = D("UTmbs");
			$where_utmbs['code_date']=array('neq',0);
			$where_utmbs['valid']=array('eq',1);
			$where_utmbs['txttime']=array('like',$year_month."%");
			$list_utmbs = $utmbsModel->where($where_utmbs)->order("pro desc ,txttime asc")->page('1,8')->select();
			foreach ($list_utmbs as $key_utmbs => $value_utmbs) {
				$data_utmbs['y_m']=$year_month;
				$data_utmbs['type']='tmbs';
				$data_utmbs['ranking']=$ranking_arr[$key_utmbs];
				$data_utmbs['ranking_num']=$key_utmbs+1;
				$data_utmbs['username']=$value_utmbs['username'];
				$data_utmbs['uid']=$value_utmbs['uid'];
				$data_utmbs['pro']=$value_utmbs['pro'];
				if($data_utmbs['pro']<80&&$data_utmbs['ranking_num']<4){
					$data_utmbs['money']=$money_utmbs[$key_utmbs]/2;
				}else{
					$data_utmbs['money']=$money_utmbs[$key_utmbs];
				}
				
				$data_utmbs['createtime']=date("Y-m-d H:i:s",time());
				$where_utmbs_find['y_m']=$year_month;
				$where_utmbs_find['type']='tmbs';
				$where_utmbs_find['ranking']=$ranking_arr[$key_utmbs];
				$find_utmbs = $jjggModel->where($where_utmbs_find)->find();
				if($find_utmbs){
					$where_utmbs_save['id']=$find_utmbs['id'];
					$save_utmbs_result=$jjggModel->where($where_utmbs_save)->save($data_utmbs);
					if($save_utmbs_result===false){$commint = false;}
				}else{
					$add_utmbs_result  = $jjggModel->add($data_utmbs);
					if($add_utmbs_result===false){$commint = false;}
				}
			}
			//特码波色统计
			$utmdsModel = D("UTmds");
			$where_utmds['code_date']=array('neq',0);
			$where_utmds['valid']=array('eq',1);
			$where_utmds['txttime']=array('like',$year_month."%");
			$list_utmds = $utmdsModel->where($where_utmds)->order("pro desc ,txttime asc")->page('1,8')->select();
			foreach ($list_utmds as $key_utmds => $value_utmds) {
				$data_utmds['y_m']=$year_month;
				$data_utmds['type']='tmds';
				$data_utmds['ranking']=$ranking_arr[$key_utmds];
				$data_utmds['ranking_num']=$key_utmds+1;
				$data_utmds['username']=$value_utmds['username'];
				$data_utmds['uid']=$value_utmds['uid'];
				$data_utmds['pro']=$value_utmds['pro'];
				if($data_utmds['pro']<80&&$data_utmds['ranking_num']<4){
					$data_utmds['money']=$money_utmds[$key_utmds]/2;
				}else{
					$data_utmds['money']=$money_utmds[$key_utmds];
				}
				$data_utmds['createtime']=date("Y-m-d H:i:s",time());
				$where_utmds_find['y_m']=$year_month;
				$where_utmds_find['type']='tmds';
				$where_utmds_find['ranking']=$ranking_arr[$key_utmds];
				$find_utmds = $jjggModel->where($where_utmds_find)->find();
				if($find_utmds){
					$where_utmds_save['id']=$find_utmds['id'];
					$save_utmds_result=$jjggModel->where($where_utmds_save)->save($data_utmds);
					if($save_utmds_result===false){$commint = false;}
				}else{
					$add_utmds_result  = $jjggModel->add($data_utmds);
					if($add_utmds_result===false){$commint = false;}
				}
			}
			dump($commint);
		if($commint==false){
			$model->rollback ();
		}else{
			$model->commit ();
		}
	}
	
	
	
	
	
}